import {defineStore} from 'pinia';
import {Http, Cache, Helper, Msg, Constant, Log} from 'core';

/**
 * 押金处理
 * @type {{id: string, state(): {}, getters: {}, actions: {}}}
 */
const store = {
    id: "deposit",
    state() {
        return {

            depositInfo: {depositType: "NORMAL"},
        }
    },

    getters: {},

    actions: {
        /**
         * 读取押金信息
         * @param orderNo
         * @returns {Promise<*|{}>}
         */
        async getInfo(orderNo) {
            Msg.loading("加载中...")
            this.depositInfo = {};
            let {code, data} = await Http.get(`/orderPay/getOrderDepositInfo/${orderNo}`);
            Msg.loading(false);
            if(code == 200) {
                this.depositInfo = data;
            }

            return this.depositInfo;
        },


        /**
         * 订单押金扣款
         * @param id
         * @param optDeposit
         * @param remark
         * @param fileArr
         */
        async deduct(id, optDeposit, remark, fileArr) {
            Msg.loading("加载中...")
            let {code, data} = await Http.post("/orderPay/deductOrderDeposit", {recordId: id, optDeposit, remark, fileArr});
            Msg.loading(false);
            if(code == 200) {
                Msg.success("扣款成功");
                return true;
            }

            return false;
        },

        /**
         * 订单押金退押
         * @param id
         * @param optDeposit
         * @param remark
         * @param fileArr
         */
        async refunded(id, optDeposit, remark, fileArr) {
            Msg.loading("加载中...")
            let {code, data} = await Http.post("/orderPay/refundedOrderDeposit", {recordId: id, optDeposit, remark, fileArr});
            Msg.loading(false);
            if(code == 200) {
                Msg.success("退押成功");
                return true;
            }

            return false;
        },

        /**
         * 订单押金解约
         * @param id
         * @param optDeposit
         * @param remark
         * @param fileArr
         */
        async unfreeze(orderNo) {
            Msg.loading("加载中...")
            let {code, data} = await Http.post("/orderPay/unfreezeAllOrderDeposit/" + orderNo);
            Msg.loading(false);
            if(code == 200) {
                Msg.success("解约成功");
                return true;
            }

            return false;
        }
    }
}

export default defineStore(store)
